package com.nou.bdm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nou.bdm.dao.NouEnumerationTDao;
import com.nou.bdm.model.NouCollageEnumerationT;
import com.nou.bdm.model.NouEnumerationT;
import com.nou.bdm.model.NouSpecInfoT;
import com.nou.bdm.model.NouStudentInfoT;
import com.nou.bdm.service.LevelService;
import com.nou.bdm.service.NouCollageEnumerationTService;
import com.nou.bdm.service.NouSpecInfoTService;
import com.nou.bdm.service.NouStudentInfoTService;
import com.nou.bdm.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.util.List;
import java.util.UUID;

@Service
public class LevelServiceImpl extends ServiceImpl<NouEnumerationTDao, NouEnumerationT> implements LevelService {


    @Override
    public PageInfo<NouEnumerationT>  getLevelList(int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        QueryWrapper<NouEnumerationT> wrapper=new QueryWrapper<>();
        wrapper.orderByDesc("type");
        List<NouEnumerationT> data = this.baseMapper.selectList(wrapper);
        PageInfo<NouEnumerationT> pageData = new PageInfo<>(data);
        return pageData;
    }

    @Override
    public String  addLevel(NouEnumerationT nouEnumerationT) {
        String type=nouEnumerationT.getType();
        String typeName=nouEnumerationT.getTypeName();
        nouEnumerationT.setItemCode(type);
        nouEnumerationT.setIsDefault("1");


      List<NouEnumerationT> data=  this.lambdaQuery().eq(NouEnumerationT::getType,type).list();
        String msg = "";
        if (data.size() > 0) {
              msg="层次代码已经存在，不能添加";
//            return Result.fail("层次代码已经存在，不能添加");
            return msg;
        }

        data=  this.lambdaQuery().eq(NouEnumerationT::getTypeName,typeName).list();
        if (data.size() > 0) {
            msg="层次名称已经存在，不能添加";
//            return Result.fail("层次名称已经存在，不能添加");
            return msg;
        }
        UUID uuid=UUID.randomUUID();
        nouEnumerationT.setId(uuid.toString().substring(0,16));
        save(nouEnumerationT);
        msg="层次添加成功！";
        return msg;

    }

    @Autowired
    NouStudentInfoTService studentInfoTService;

    @Autowired
    NouCollageEnumerationTService collageEnumerationTService;

    @Autowired
    NouSpecInfoTService specInfoTService;

    @Override
    public String deleteLevel(String  id) {
        NouEnumerationT nouEnumerationT = getById(id);

        List<NouStudentInfoT> studentInfoTS = studentInfoTService.lambdaQuery()
                .eq(NouStudentInfoT::getEducationLevel, nouEnumerationT.getTypeName()).list();
        String msg = "";
        if (studentInfoTS.size() > 0) {
            msg="学生使用层次代码，不能删除";
            return msg;
        }

        List<NouCollageEnumerationT> collageEnumerationTS=collageEnumerationTService.lambdaQuery()
                .eq(NouCollageEnumerationT::getType,nouEnumerationT.getType()).list();
        if (collageEnumerationTS.size() > 0) {
            msg="院校使用层次代码，不能删除";
            return msg;
        }

        List<NouSpecInfoT> specInfoTS = specInfoTService.lambdaQuery()
                .eq(NouSpecInfoT::getEducationLevel, nouEnumerationT.getTypeName()).list();
        if (specInfoTS.size() > 0) {
            msg="专业使用层次代码，不能删除";
            return msg;
        }

        this.removeById(id);
        msg = "删除成功！";

        return msg;
    }

    @Override
    public String updateLevel(NouEnumerationT nouEnumerationT) {
        String type=nouEnumerationT.getType();
        String typeName=nouEnumerationT.getTypeName();
        this.saveOrUpdate(nouEnumerationT);

//
//        List<NouEnumerationT> data=  this.lambdaQuery().eq(NouEnumerationT::getType,type).list();
//        String msg = "";
//        if (data.size() > 0) {
//            msg="层次代码已经存在，不能添加";
////            return Result.fail("层次代码已经存在，不能添加");
//            return msg;
//        }
//
//        data=  this.lambdaQuery().eq(NouEnumerationT::getTypeName,typeName).list();
//        if (data.size() > 0) {
//            msg="层次名称已经存在，不能添加";
////            return Result.fail("层次名称已经存在，不能添加");
//            return msg;
//        }
        return "层次修改成功！";
    }
}
